/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.dao;

import org.ipti.br.tag.entity.PerformanceFrequency;
import org.ipti.br.tag.entity.PerformanceFrequency_;
import org.ipti.br.tag.entity.Plan_;
import java.util.Calendar;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.ipti.br.tag.util.EncryptionUtil;

/**
 *
 * @author israel
 */
@Stateless
public class PerformanceFrequencyFacade extends AbstractFacade<PerformanceFrequency> {

    @PersistenceContext(unitName = "TAG2PU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public PerformanceFrequencyFacade() {
        super(PerformanceFrequency.class);
    }

    @Override
    public void create(PerformanceFrequency performanceFrequency) {
        getEntityManager().persist(performanceFrequency);
    }

    @Override
    public void edit(PerformanceFrequency performanceFrequency) {
        getEntityManager().merge(performanceFrequency);
    }

    public List<PerformanceFrequency> findPerformanceFrequencyPerPlan(Long planID) {
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<PerformanceFrequency> cq = cb.createQuery(PerformanceFrequency.class);
        Root<PerformanceFrequency> from = cq.from(PerformanceFrequency.class);
        Predicate predicate = cb.equal(from.get(PerformanceFrequency_.plan).get(Plan_.id), planID);
        cq.where(cb.and(predicate));

        return getEntityManager().createQuery(cq).getResultList();
    }

    public List<PerformanceFrequency> modifiedPerfFreqBetweenDate(Calendar initialDate, Calendar finalDate, Long serialNumber) {
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<PerformanceFrequency> cq = cb.createQuery(PerformanceFrequency.class);
        Root<PerformanceFrequency> from = cq.from(PerformanceFrequency.class);
        Predicate predicate1 = cb.notEqual(from.get(PerformanceFrequency_.serialNumber), serialNumber);
        Predicate predicate2 = cb.greaterThanOrEqualTo(from.get(PerformanceFrequency_.lastModified), initialDate);
        Predicate predicate3 = cb.lessThan(from.get(PerformanceFrequency_.lastModified), finalDate);
        cq.where(cb.and(predicate1, predicate2, predicate3));

        return getEntityManager().createQuery(cq).getResultList();
    }
}
